/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.epsi.blokus.entity;

import java.awt.Graphics2D;
import org.epsi.blokus.tools.IConstantes;
import org.epsi.blokus.tools.Log;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Afficher un avatar
 * @author julien.lafont
 */
public class PlayerAvatar implements IEntity, IConstantes {

    // Etat en cours
    private int currentAvatarIndex = 0;
    private AVATAR_T_ETATS currentAvatarEtat = AVATAR_T_ETATS.Neutral;
    private Image currentAvatarImg = null;

    // Position
    private float x;
    private float y;

    // Calcul du temps pour le petit effet
    private int totalDelta = 0;

    @Override
    public void init(GameContainer container, StateBasedGame game) throws SlickException {
        Log.out("PlayerAvatar : init");

        reloadImage();

    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
        totalDelta += delta;

        // Afficher l'image 'Thinking' 1s toutes les 5s
        if (totalDelta > 4000) {
            currentAvatarEtat = AVATAR_T_ETATS.Thinking;
            reloadImage();
        }
        else if ( currentAvatarEtat == AVATAR_T_ETATS.Thinking && totalDelta > 1000)
        {
            currentAvatarEtat = AVATAR_T_ETATS.Neutral;
            reloadImage();
        }
    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        if (currentAvatarImg!=null) g.drawImage(currentAvatarImg, x, y);
    }

    /**
     * Recharge l'image à afficher en mémoire
     */
    private void reloadImage()
    {
        Log.out("PlayerAvatar : reloadImage");

        try {
            String path = AVATAR_FOLDER + "Portrait." + AVATAR_LIST[currentAvatarIndex]+"."+currentAvatarEtat.toString()+".png";
            currentAvatarImg = new Image(path);
            totalDelta = 0;
        }
        catch(SlickException e)
        {
            currentAvatarImg = null;
            System.err.println("Impossible de charger l'avatar");
            System.err.println(e.getMessage());
        }
    }

    /**
     * Define the absolute position of the element
     * @param x
     * @param y
     */
    public void setPosition(float x, float y)
    {
        this.x = x;
        this.y = y;
    }

    /**
     * Render the next avatar
     */
    public void avatarNext()
    {
        if ((currentAvatarIndex+1) < AVATAR_LIST.length) currentAvatarIndex++;
        else currentAvatarIndex = 0;

        reloadImage();
    }

    /**
     * Render the previous avatar
     */
    public void avatarPrevious()
    {
        if (currentAvatarIndex != 0) currentAvatarIndex--;
        else currentAvatarIndex = AVATAR_LIST.length-1;

        reloadImage();
    }

    /**
     * Retourne l'avatar actuellement sélectionné
     * @return
     */
    public String getSelectedAvatar()
    {
        return AVATAR_LIST[currentAvatarIndex];
    }

}
